home *** CD-ROM | disk | FTP | other *** search
/ The Fatted Calf / The Fatted Calf.iso / Applications / Graphics / Pyramid / Source / SpatialView.m < prev    next >
Text File  |  1993-09-15  |  3KB  |  175 lines

  1. /*    SpatialView.m - Display 3D image
  2.  *    Copyright (C) 1993 Corona Design, Inc. All rights reserved.
  3.  *
  4.  *    Abstract
  5.  *        Manages the dynamic display of a 3D scene.
  6.  *
  7.  *    RCS path: 
  8.  *        $Source: /Users/pkron/Projects/voxel/Pyramid/RCS/SpatialView.m,v $
  9.  *    Modified: $Date: 93/09/15 12:35:17 $ by $Author: pkron $
  10.  *    Current State: $State: Exp $ locked by $Locker:  $
  11.  */
  12.  
  13. #import     "SpatialView.h"
  14.  
  15. #import        "TransformController.h"
  16. #import        <math.h>
  17.  
  18.  
  19.  
  20. #define    SQRT2    1.414214    
  21. #define    SQRT3    1.732051
  22. #define    SQRT6    (SQRT2*SQRT3)
  23.  
  24. static POINT    moon3D = { .3, .3, .7, 1};
  25.     
  26. @implementation SpatialView
  27.  
  28.  
  29. - initFrame: (const NXRect *)newFrame
  30.     {
  31.     float    drawSize = 1.;
  32.     
  33.     [super initFrame: newFrame];
  34.  
  35.                                 // draw in a unit space with
  36.                                 // origin at center    
  37.     [self setDrawSize: drawSize : drawSize];
  38.     [self translate: drawSize/2 : drawSize/2];
  39.     clip = bounds;
  40.  
  41.                                 // initial (dummy) transformation
  42.     transform = allocIdentity();
  43.     return( self);
  44.     }
  45.     
  46. - free
  47.     {
  48.     freeMatrix( transform);
  49.     return( [super free]);
  50.     }
  51.  
  52.  
  53. #define    DISTANCE    ([distance floatValue]+1)
  54.  
  55.                                 // react to change in viewing angle
  56. - changePerspective: sender
  57.     {
  58.     [self display];
  59.     return( self);
  60.     }
  61.     
  62.  
  63.                                 // draw circle about the moon center
  64. - drawMoon: (NXRect *)moonClip;
  65.     {
  66.     float    radius;
  67.  
  68.     if ( [showMoon floatValue] )
  69.         {
  70.         radius = .1/(moon[2]);
  71.         project( &moon);
  72.         
  73.         PSsetgray( .8);
  74.         PSmoveto( moon[0], moon[1]);
  75.         PSarc( moon[0], moon[1], radius, 0, 360);
  76.         PSclosepath();
  77.         PSfill();
  78.         
  79.         NXSetRect( moonClip, moon[0] - radius, moon[1] - radius, 
  80.                     2*radius, 2*radius
  81.                     );
  82.         }
  83.         
  84.     return( self);
  85.     }    
  86.     
  87.  
  88. - drawSelf: (const NXRect *)rects : (int)count
  89.     {
  90.     BOOL    moonDrawn = NO;
  91.     POINT    origin, zenith, reflectedMoon;
  92.     NXRect    tmpRect;
  93.     
  94.     [super drawSelf: rects : count];
  95.     
  96.                                 // erase last image
  97.     PSsetrgbcolor( 1, 1, .6);    // pale yellow
  98.     NXRectFill( &clip);
  99.  
  100.     transform = [transformController transform];
  101.  
  102.                                 // map moon to view space
  103.     moon[0] = moon3D[0];    
  104.     moon[1] = moon3D[1];    
  105.     moon[2] = moon3D[2];    
  106.     moon[3] = moon3D[3];
  107.     map( transform, &moon);    
  108.  
  109.                                 // cheap hidden moon algorithm
  110.                                 // doesn't work completely
  111.     origin[0] = 0;
  112.     origin[1] = 0;
  113.     origin[2] = -1;
  114.     origin[3] = 1;
  115.     zenith[0] = 0;
  116.     zenith[1] = 0;
  117.     zenith[2] = 0;
  118.     zenith[3] = 1;
  119.     reflectedMoon[0] = -moon3D[0];
  120.     reflectedMoon[1] = -moon3D[1];
  121.     reflectedMoon[2] = moon3D[2];
  122.     reflectedMoon[3] = 1;
  123.     map( transform, &origin);    
  124.     map( transform, &zenith);    
  125.     map( transform, &reflectedMoon);    
  126.  
  127.                                 // if view is from below xy plane
  128.                                 // or moon is behind its z-axix projection    
  129.     if ( origin[2] LT zenith[2] || moon[2] GT reflectedMoon[2] )
  130.         {
  131.         [self drawMoon: &tmpRect];
  132.         moonDrawn = YES;
  133.         }
  134.  
  135.                                 // draw the scene model
  136.     [model drawModel: transform : &clip];
  137.  
  138.                                 // if moon in front of view plane    
  139.     if ( !moonDrawn )
  140.         [self drawMoon: &tmpRect];
  141.  
  142.     if ( 
  143.             [showVanishingPoints intValue] 
  144.             && [model respondsTo: @selector( drawVanishingPoints::)]
  145.             )
  146.         [model drawVanishingPoints: transform : &clip];
  147.         
  148.     NXUnionRect( &tmpRect, &clip);    
  149.     return( self);
  150.     }
  151.     
  152.  
  153.                                 // select a new model for scene
  154. - useModel: anObject
  155.     {
  156.     [model free];
  157.     model = anObject;
  158.     clip = bounds;
  159.     [self display];
  160.  
  161.     return( self);
  162.     }
  163.     
  164.  
  165. @end
  166.  
  167. #ifdef    _LOG
  168. /*
  169.  *    $Log:    SpatialView.m,v $
  170. Revision 1.1  93/09/15  12:35:17  pkron
  171. Created.
  172.  
  173.  */
  174. #endif        
  175.